package com.hmkj.admin.controller.amount;

import com.hmkj.admin.controller.BaseController;
import com.hmkj.admin.controller.GenericController;
import com.hmkj.common.entity.ResultEntity;
import com.hmkj.common.entity.dtgrid.DtGrid;
import com.hmkj.common.entity.export.ExportUtils;
import com.hmkj.core.constant.Constants;
import com.hmkj.core.exception.BussinessException;
import com.hmkj.core.model.amount.CashModel;
import com.hmkj.core.po.amount.Cash;
import com.hmkj.core.service.amount.CashService;
import com.xiaoleilu.hutool.util.StrUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;

/**
 * 提现审核记录
 *
 * @author zhaxiaowei
 * @create 2017-06-21 21:02
 **/
@Controller
@RequestMapping(BaseController.BASE_URI)
public class CashJsonController extends GenericController<Integer, Cash> {

    @Resource
    private CashService cashService;

    /**
     * 提现审核记录列表json数据
     *
     * @param dtGridPager 列表对象
     * @return DtGrid
     * @throws Exception ex
     */
    @ResponseBody
    @RequestMapping("cash/json/list")
    @RequiresPermissions("sys:cash:list")
    public DtGrid listJson(String dtGridPager) throws Exception {
        DtGrid dtgrid = dataAuthority(dtGridPager, Constants.AG);
        return cashService.getDtGridList(dtgrid, CashModel.class);
    }

    /**
     * 提现审核
     *
     * @param cashModel 提现记录对象
     * @return ResultEntity
     * @throws Exception ex
     */
    @ResponseBody
    @RequestMapping("cash/json/audit")
    @RequiresPermissions("sys:cash:audit")
    public ResultEntity doAudit(CashModel cashModel) throws Exception {
        if(null == cashModel || cashModel.getId() == null || cashModel.getId() <= 0){
            throw new BussinessException("参数错误");
        }
        //获取备注内容的长度
        if (StrUtil.isBlank(cashModel.getRemark())) {
            throw new BussinessException("【审核备注】不能为空");
        }
        int len = 255;
        if (cashModel.getRemark().length() > len) {
            throw new BussinessException("【审核备注】输入的内容长度超过容量，请缩短输入的内容");
        }
        //状态校验
        boolean b = cashModel.getAuditStatus() != CashModel.AUDITSTATUS.T1.code && cashModel.getAuditStatus() != CashModel.AUDITSTATUS.T2.code;
        if(null == cashModel.getAuditStatus() || b){
            throw new BussinessException("审核状态有误");
        }
//        cashService.doAudit(cashModel, "api");
//        // 连连代付提现
//        cashService.doAuditLianLian(cashModel);
        // 华融代付提现
        cashService.doAuditHR(cashModel);
        return ok();
    }

    /**
     * 提现取消
     *
     * @param  id 提现记录ID
     * @return ResultEntity
     * @throws Exception ex
     */
    @ResponseBody
    @RequestMapping("cash/json/cancel")
    public ResultEntity doCancel(Integer id) throws Exception {
        if (id == null || id <= 0) {
            throw new BussinessException("参数错误");
        }
        cashService.doCancel(id);

        return ok();
    }


    /**
     * 列表导出
     *
     * @param  dtGridPager 列表对象
     * @return ResultEntity
     */
    @ResponseBody
    @RequestMapping(value = "cash/json/export", method = RequestMethod.POST)
    public ResultEntity export(String dtGridPager, HttpServletRequest request, HttpServletResponse response) throws Exception {
        // 执行导出
        ExportUtils.export(request, response, cashService.getDtGridListExport(dtGridPager,CashModel.class));
        return ok();
    }

    @ResponseBody
    @RequestMapping("cash/json/hand")
    @RequiresPermissions("sys:user:offline:cash")
    public ResultEntity hand(Cash cash) throws Exception {
        //校验
        String result = checkCash(cash);
        if(result!=null){
            throw  new BussinessException(result);
        }
        cash.setAmount(cash.getAmount().setScale(2, BigDecimal.ROUND_HALF_DOWN));
        cashService.handCash(cash);
        return ok();
    }


    private  String checkCash(Cash cash) {
        String result = null;
        if (cash.getUserId()==null){
            result = "用户id 不能为空";
        }else if(cash.getType()==null) {
            result = "请选择用途";
        }else if(cash.getOutOrderNo()==null||cash.getOutOrderNo().equals("")){
            result = "请填写订单号";
        }else if(cash.getAmount()==null){
            result = "请输入金额";
        }else if(cash.getRemark()==null||cash.getRemark().equals("")){
            result = "请填写备注";
        }
        return result;
    }

}
